class Formatter

  # Public interface

  def initialize()
    @lines = []
  end

  def report_range(start_date, end_date)
    @start_date = start_date
    @end_date = end_date
  end

  def use_subsystem_with_change_count(subsystem_name, change_count)
    @lines << subsystem_line(subsystem_name, change_count)
  end

  def output
    [header] + order_by_descending_change_count
  end

  # Helpers

  def subsystem_line(subsystem_name, change_count)
    image = image_for(change_count)
    text = text_for(change_count)
    "#{subsystem_name.ljust(14)} #{text.ljust(14)} #{image}"
  end

  def image_for(an_integer)
    return "-" if an_integer == 0
    return "*" if an_integer < 3
    "*" *  (an_integer / 5.to_f).round
  end

  def text_for(an_integer)
    (an_integer == 0)? "-" : "(#{an_integer} changes)"
  end

  def churn_line_to_int(line)
    /\((\d+)\s+\w+\)/.match(line)[1].to_i
  end

  def order_by_descending_change_count
    @lines.sort do |one, another|
      - (churn_line_to_int(one) <=> churn_line_to_int(another))
    end
  end

  def header
    "Changes between #{date(@start_date)} and #{date(@end_date)}:"
  end

  def date(a_time)
    a_time.strftime("%Y-%m-%d")
  end

  def interesting(lines)
    lines.find_all do | line |
      /\s\*+/.match(line)
    end

  end

end